package com.ruoyi.exam.controller;

import java.util.List;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.exam.domain.ExamUserInfo;
import com.ruoyi.exam.domain.model.LabelModel;
import com.ruoyi.exam.service.IExamUserInfoService;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;

/**
 * 用户分数信息管理Controller
 * 
 * @author wky
 * @date 2024-09-19
 */
@RestController
@RequestMapping("/exam/info")
@Tag(name = "【用户分数信息管理】管理")
public class ExamUserInfoController extends BaseController {
    @Autowired
    private IExamUserInfoService examUserInfoService;

    /**
     * 查询用户分数信息管理列表
     */
    @Operation(summary = "查询用户分数信息管理列表")
    @PreAuthorize("@ss.hasPermi('exam:info:list')")
    @GetMapping("/list")
    public TableDataInfo list(ExamUserInfo examUserInfo) {
        startPage();
        List<ExamUserInfo> list = examUserInfoService.selectExamUserInfoList(examUserInfo);
        return getDataTable(list);
    }

    /**
     * 导出用户分数信息管理列表
     */
    @Operation(summary = "导出用户分数信息管理列表")
    @PreAuthorize("@ss.hasPermi('exam:info:export')")
    @Log(title = "用户分数信息管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ExamUserInfo examUserInfo) {
        List<ExamUserInfo> list = examUserInfoService.selectExamUserInfoList(examUserInfo);
        ExcelUtil<ExamUserInfo> util = new ExcelUtil<ExamUserInfo>(ExamUserInfo.class);
        util.exportExcel(response, list, "用户分数信息管理数据");
    }

    /**
     * 获取用户分数信息管理详细信息
     */
    @Operation(summary = "获取用户分数信息管理详细信息")
    @PreAuthorize("@ss.hasPermi('exam:info:query')")
    @GetMapping(value = "/{userId}")
    public AjaxResult getInfo(@PathVariable("userId") Long userId) {
        return success(examUserInfoService.selectExamUserInfoByUserId(userId));
    }

    @GetMapping("/getScore")
    public AjaxResult getScore() {
        ExamUserInfo selectExamUserInfoByUserId = examUserInfoService.selectExamUserInfoByUserId(getUserId());
        if (selectExamUserInfoByUserId == null) {
            return success();
        }
        return success(JSONArray.parse(selectExamUserInfoByUserId.getScoreJson()));
    }

    /**
     * 新增用户分数信息管理
     */
    @Operation(summary = "新增用户分数信息管理")
    @PreAuthorize("@ss.hasPermi('exam:info:add')")
    @Log(title = "用户分数信息管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ExamUserInfo examUserInfo) {
        examUserInfoService.deleteExamUserInfoByUserId(getUserId());
        return toAjax(examUserInfoService.insertExamUserInfo(examUserInfo));
    }

    /**
     * 修改用户分数信息管理
     */
    @Operation(summary = "修改用户分数信息管理")
    @PreAuthorize("@ss.hasPermi('exam:info:edit')")
    @Log(title = "用户分数信息管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ExamUserInfo examUserInfo) {
        return toAjax(examUserInfoService.updateExamUserInfo(examUserInfo));
    }

    /**
     * 删除用户分数信息管理
     */
    @Operation(summary = "删除用户分数信息管理")
    @PreAuthorize("@ss.hasPermi('exam:info:remove')")
    @Log(title = "用户分数信息管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{userIds}")
    public AjaxResult remove(@PathVariable(name = "userIds") Long[] userIds) {
        return toAjax(examUserInfoService.deleteExamUserInfoByUserIds(userIds));
    }

    @PostMapping("/importScores")
    public AjaxResult importUserScore(@RequestBody List<LabelModel> scores) {
        ExamUserInfo examUserInfo = new ExamUserInfo();
        examUserInfo.setCreateBy(getUsername());
        examUserInfo.setCreateTime(DateUtils.getNowDate());
        examUserInfo.setUserId(getUserId());
        examUserInfo.setScoreJson(JSON.toJSONString(scores));
        examUserInfoService.insertExamUserInfo(examUserInfo);
        return success();
    }
}
